home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / xgui / sources / bn_JordElm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  30.4 KB  |  1,176 lines

  1. /*****************************************************************************
  2.   FILE           : bn_JordElm.c
  3.   SHORTNAME      : bn_JordElm.c
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : Bignet panel for Jordan and Elman networks
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Tobias Soyez
  10.   DATE           : 09.11.1993
  11.  
  12.   CHANGED BY     : 
  13.   IDENTIFICATION : @(#)bn_JordElm.c    1.3 3/2/94
  14.   SCCS VERSION   : 1.3
  15.   LAST CHANGE    : 3/2/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/Xos.h>
  27. #include <X11/cursorfont.h>
  28. #include <X11/Intrinsic.h>
  29. #include <X11/StringDefs.h>
  30. #include <X11/Shell.h>
  31. #include <X11/Xaw/Box.h>
  32. #include <X11/Xaw/Simple.h>
  33. #include <X11/Xaw/Grip.h>
  34. #include <X11/Xaw/Form.h>
  35. #include <X11/Xaw/SmeBSB.h>
  36. #include <X11/Xaw/SmeLine.h>
  37. #include <X11/Xaw/Viewport.h>
  38. #include <X11/Xaw/Label.h>
  39. #include <X11/Xaw/Toggle.h>
  40. #include <X11/Xaw/Command.h>
  41. #include <X11/Xaw/Cardinals.h>
  42. #include <X11/Xaw/AsciiText.h>
  43. #include <X11/Xaw/Scrollbar.h>
  44.  
  45. #include "ui.h"
  46. #include "kr_const.h"
  47. #include "glob_typ.h"
  48. #include "kr_ui.h"
  49. #include "kr_typ.h"
  50. #include "kr_def.h"
  51. #include "kernel.h"
  52. #include "ui_mainP.h"
  53. #include "ui_confirmer.h"
  54. #include "ui_textP.h"
  55. #include "ui_utilP.h"
  56. #include "ui_netUpdate.h"
  57. #include "ui_status.h"
  58. #include "ui_xWidgets.h"
  59. #include "ui_fileP.h"
  60. #include "ui_display.h"
  61. #include "ui_event.h"
  62. #include "ui_selection.h"
  63. #include "bn_basics.h"
  64. #include "bn_JordElm.ph"
  65.  
  66.  
  67.  
  68. /*****************************************************************************
  69.   FUNCTION : createUnitLayer
  70.  
  71.   PURPOSE  : creates a layer of no_of_units units 
  72.   RETURNS  : the number of the first unit of the created layer or a kernel
  73.              error code
  74.   NOTES    :
  75.  
  76.   UPDATE   : 
  77. ******************************************************************************/
  78.  
  79. static int createUnitLayer (int  no_of_units, 
  80.                   int  io_type,
  81.                     char *act_func_name,
  82.                 char *out_func_name,
  83.                 char *unit_name)
  84.  
  85. {
  86.   krui_err     error_code = KRERR_NO_ERROR ;
  87.   int          i, unit_no, first_unit ;
  88.   char         name[10] ;
  89.   struct Unit *unit_ptr ;
  90.  
  91.   
  92.   if (no_of_units < 1) return (KRERR_NO_UNITS) ;
  93.  
  94.   for (i = 1 ; i <= no_of_units ; i++)
  95.   {
  96.     sprintf (name, "%s%d", unit_name, i) ;
  97.  
  98.     unit_no = krui_createUnit (name, out_func_name, act_func_name, 0, 0) ; 
  99.     if (unit_no < 0) IF_ERROR_RETURN (unit_no) ;
  100.  
  101.     if (i == 1) first_unit = unit_no ;
  102.  
  103.     error_code = krui_setUnitTType (unit_no, io_type) ;
  104.  
  105.     IF_ERROR_RETURN (error_code) ;
  106.   }
  107.  
  108.   return (first_unit) ;
  109. }
  110.  
  111.  
  112.  
  113. /*****************************************************************************
  114.   FUNCTION : connectFull
  115.  
  116.   PURPOSE  : connects every unit of one set (no_of_source_units units, 
  117.              beginning with from_unit) to every unit of a second set 
  118.              no_of_target_units units, beginning with to_unit)
  119.   RETURNS  : kernel error code
  120.   NOTES    :
  121.  
  122.   UPDATE   : 
  123. ******************************************************************************/
  124.  
  125. static krui_err connectFull (int       from_unit,
  126.                  int       no_of_source_units,
  127.                  int       to_unit,
  128.                  int       no_of_target_units,
  129.                  FlintType weight) 
  130.  
  131. {
  132.   krui_err  error_code = KRERR_NO_ERROR ;
  133.   int       i, j ;
  134.  
  135.   
  136.   for (i = to_unit ; i < to_unit + no_of_target_units ; i++)
  137.   {
  138.     error_code = krui_setCurrentUnit (i) ;
  139.     IF_ERROR_RETURN (error_code) ;
  140.  
  141.     for (j = from_unit ; j < from_unit + no_of_source_units ; j++)
  142.     {
  143.       error_code = krui_createLink (j, weight) ;
  144.       IF_ERROR_RETURN (error_code) ;
  145.     } 
  146.   }
  147.  
  148.   return (error_code) ;
  149. }
  150.  
  151.  
  152.  
  153. /*****************************************************************************
  154.   FUNCTION : connectOneToOne
  155.  
  156.   PURPOSE  : connects every unit of one set (no_of_units units, beginning 
  157.              with from_unit) to the corresponding unit in the second set
  158.              (no_of_units units, beginning with to_unit)
  159.   RETURNS  : kernel error code
  160.   NOTES    :
  161.  
  162.   UPDATE   :
  163. ******************************************************************************/
  164.  
  165. static krui_err connectOneToOne (int       from_unit,
  166.                      int       to_unit,
  167.                      int       no_of_units,
  168.                      FlintType weight) 
  169.  
  170. {
  171.   krui_err  error_code = KRERR_NO_ERROR ;
  172.   int       i ;
  173.  
  174.  
  175.   for (i = 0 ; i < no_of_units ; i++)
  176.   {
  177.     error_code = krui_setCurrentUnit (to_unit + i) ;
  178.     IF_ERROR_RETURN (error_code) ;
  179.  
  180.     error_code = krui_createLink (from_unit + i, weight) ;
  181.     IF_ERROR_RETURN (error_code) ;
  182.   }
  183.  
  184.   return (error_code) ;
  185. }
  186.  
  187.  
  188.  
  189. /*****************************************************************************
  190.   FUNCTION : connectSelfRec
  191.  
  192.   PURPOSE  : connects every unit of a set (no_of_units units, beginning with
  193.              unit_no) to itself
  194.   RETURNS  : kernel error code
  195.   NOTES    :
  196.  
  197.   UPDATE   : 
  198. ******************************************************************************/
  199.  
  200. static krui_err connectSelfRec (int       unit_no,
  201.                     int       no_of_units,
  202.                     FlintType weight) 
  203.  
  204. {
  205.   krui_err  error_code = KRERR_NO_ERROR ;
  206.   int       i ;
  207.  
  208.  
  209.   for (i = unit_no ; i < unit_no + no_of_units ; i++)
  210.   {
  211.     error_code = krui_setCurrentUnit (i) ;
  212.     IF_ERROR_RETURN (error_code) ;
  213.  
  214.     error_code = krui_createLink (i, weight) ;
  215.     IF_ERROR_RETURN (error_code) ;
  216.   }
  217.   return (error_code) ;
  218. }
  219.  
  220.  
  221.  
  222. /*****************************************************************************
  223.   FUNCTION : setPosRow
  224.  
  225.   PURPOSE  : sets the positions of a set of units (no_of_units units,
  226.              beginning with unit_no) starting at (x, y) with max. r units
  227.              per row
  228.   RETURNS  : void
  229.   NOTES    :
  230.  
  231.   UPDATE   : 
  232. ******************************************************************************/
  233.  
  234. static void setPosRow (int unit_no, int no_of_units, int x, int y, int r)
  235.  
  236. {
  237.   struct PosType  unit_pos ;
  238.   int             col, row ;
  239.   int             i ;
  240.  
  241.  
  242.   col = x ;
  243.   row = y ;
  244.  
  245.   for (i = unit_no ; i < unit_no + no_of_units ; i++)
  246.   {
  247.     unit_pos.x = col ;
  248.     unit_pos.y = row ;
  249.     krui_setUnitPosition (i, &unit_pos) ;
  250.     col++ ;
  251.     if ((col - x) == r)
  252.     {
  253.       col = x ;
  254.       row++   ;
  255.     }
  256.   }
  257. }
  258.  
  259.  
  260.  
  261. /*****************************************************************************
  262.   FUNCTION : setPosCol
  263.  
  264.   PURPOSE  : sets the positions of a set of units (no_of_units units,
  265.              beginning with unit_no) starting at (x, y) with max. c units
  266.              per column
  267.   RETURNS  : void
  268.   NOTES    :
  269.  
  270.   UPDATE   : 
  271. ******************************************************************************/
  272.  
  273. static void setPosCol (int unit_no, int no_of_units, int x, int y, int c)
  274.  
  275. {
  276.   struct PosType  unit_pos ;
  277.   int             col, row ;
  278.   int             i ;
  279.  
  280.  
  281.   col = x ;
  282.   row = y ;
  283.  
  284.   for (i = unit_no ; i < unit_no + no_of_units ; i++)
  285.   {
  286.     unit_pos.x = col ;
  287.     unit_pos.y = row ;
  288.     krui_setUnitPosition (i, &unit_pos) ;
  289.     row++ ;
  290.     if ((row - y) == c)
  291.     {
  292.       row = y ;
  293.       col++   ;
  294.     }
  295.   }
  296. }
  297.  
  298.  
  299.  
  300. /*****************************************************************************
  301.   FUNCTION : fixWindowSize 
  302.  
  303.   PURPOSE  : fixes the window size 
  304.   RETURNS  : void
  305.   NOTES    :
  306.  
  307.   UPDATE   :
  308. ******************************************************************************/
  309.  
  310. static void fixWindowSize (Widget window)
  311.  
  312. {
  313.   Dimension  width, height ;
  314.   int        n ;
  315.   Arg        args[10] ;
  316.  
  317.  
  318.   XtMapWidget (baseWidget) ;
  319.  
  320.   n = 0 ;
  321.   XtSetArg    (args[n], XtNwidth , &width ) ; n++ ;  
  322.   XtSetArg    (args[n], XtNheight, &height) ; n++ ;
  323.   XtGetValues (window , args, n) ;
  324.  
  325.   n = 0 ;
  326.   XtSetArg    (args[n], XtNminWidth , width ) ; n++ ;  
  327.   XtSetArg    (args[n], XtNminHeight, height) ; n++ ;
  328.   XtSetArg    (args[n], XtNmaxWidth , width ) ; n++ ;  
  329.   XtSetArg    (args[n], XtNmaxHeight, height) ; n++ ;
  330.  
  331.   XtSetValues (window , args, n) ;
  332. }
  333.  
  334.  
  335.  
  336. /*****************************************************************************
  337.   FUNCTION : bn_jordan_createNet
  338.  
  339.   PURPOSE  : generates the JORDAN network 
  340.   RETURNS  : kernel error code
  341.   NOTES    :
  342.  
  343.   UPDATE   :
  344. ******************************************************************************/
  345.  
  346. static krui_err bn_jordan_createNet (int IUnits, int HUnits, int OUnits,
  347.                                      int ICols , int HCols , int OCols )
  348.  
  349. {
  350.   krui_err  error_code = KRERR_NO_ERROR;
  351.   int       i_unit, h_unit, o_unit, s_unit ;
  352.   int       idy, hdy, maxdy ;
  353.  
  354.  
  355.   /* ---------------------------   create units  ----------------------------*/
  356.  
  357.   i_unit  =
  358.   createUnitLayer (IUnits, INPUT,     "Act_Identity", "Out_Identity", "inp") ;
  359.   if (i_unit < 0) IF_ERROR_RETURN (i_unit) ;
  360.  
  361.   h_unit =
  362.   createUnitLayer (HUnits, HIDDEN,    "Act_Logistic", "Out_Identity", "hid") ;
  363.   if (h_unit < 0) IF_ERROR_RETURN (h_unit) ;
  364.  
  365.   o_unit =
  366.   createUnitLayer (OUnits, OUTPUT,    "Act_Logistic", "Out_Identity", "out") ;
  367.   if (o_unit < 0) IF_ERROR_RETURN (o_unit) ;
  368.  
  369.   s_unit  =
  370.   createUnitLayer (OUnits, SPECIAL_H, "Act_Identity", "Out_Identity", "con") ;
  371.  
  372.   if (s_unit < 0) IF_ERROR_RETURN (s_unit) ;
  373.  
  374.  
  375.   /* ---------------------------   create links  ----------------------------*/
  376.  
  377.   error_code = connectFull     (i_unit, IUnits, h_unit, HUnits, 0.0) ;
  378.   IF_ERROR_RETURN (error_code) ;
  379.  
  380.   error_code = connectFull     (h_unit, HUnits, o_unit, OUnits, 0.0) ;
  381.   IF_ERROR_RETURN (error_code) ;
  382.  
  383.   error_code = connectFull     (s_unit, OUnits, h_unit, HUnits, 0.0) ;
  384.   IF_ERROR_RETURN (error_code) ;
  385.  
  386.   error_code = connectOneToOne (o_unit, s_unit, OUnits, 0.0) ;
  387.   IF_ERROR_RETURN (error_code) ;
  388.  
  389.   error_code = connectSelfRec  (s_unit, OUnits, 0.0) ; 
  390.   IF_ERROR_RETURN (error_code) ;
  391.  
  392.  
  393.   /* -------------  calculate y-coordinate of state layer  ------------------*/
  394.  
  395.   idy = (int) ((IUnits + ICols - 1) / ICols) ;
  396.   hdy = (int) ((HUnits + HCols - 1) / HCols) ;
  397.  
  398.   if (idy > hdy) maxdy = idy ;
  399.   else           maxdy = hdy ;
  400.  
  401.  
  402.   /*  --------------------------  set positions  ----------------------------*/
  403.  
  404.   setPosRow (i_unit, IUnits, 1                  , 1        , ICols) ;
  405.   setPosRow (h_unit, HUnits, 5+ICols+OCols      , 1        , HCols) ;
  406.   setPosRow (o_unit, OUnits, 9+ICols+HCols+OCols, 1        , OCols) ;
  407.   setPosRow (s_unit, OUnits, 3+ICols            , 1 + maxdy, OCols) ;
  408.  
  409.  
  410.   /*  -----------------------  set default functions  -----------------------*/
  411.  
  412.   error_code = krui_setLearnFunc ("JE_BP") ;
  413.   IF_ERROR_RETURN (error_code) ;
  414.  
  415.   error_code = krui_setInitialisationFunc ("JE_Weights") ;
  416.   IF_ERROR_RETURN (error_code) ;
  417.  
  418.   error_code = krui_setUpdateFunc ("JE_Order") ;
  419.  
  420.   return (error_code) ;
  421.  
  422.  
  423.  
  424. /*****************************************************************************
  425.   FUNCTION : bn_jordan_donePROC
  426.  
  427.   PURPOSE  : callback function for DONE button in 'Bignet (Jordan)' 
  428.   NOTES    :
  429.  
  430.   UPDATE   : 
  431. ******************************************************************************/
  432.  
  433. static void bn_jordan_donePROC (void)
  434.  
  435. {
  436.    XtDestroyWidget (baseWidget) ;
  437.    bn_jordan_open = 0 ;
  438. }
  439.  
  440.  
  441.  
  442. /*****************************************************************************
  443.   FUNCTION : bn_jordan_createPROC
  444.  
  445.   PURPOSE  : callback function for CREATE NET button in 'BigNet (Jordan)'
  446.   NOTES    :
  447.  
  448.   UPDATE   : 
  449. ******************************************************************************/
  450.  
  451. static void bn_jordan_createPROC (void)
  452.  
  453. {
  454.   int  units[3], cols[3] ;
  455.   int  i ;
  456.   char buf[80] ;
  457.  
  458.  
  459.   if (bn_basics_check_existingNetwork()) 
  460.   {
  461.     /* -------------------------  get values  ------------------------------ */
  462.  
  463.     for (i=0; i<3; i++)
  464.     {
  465.       units[i] = ui_xIntFromAsciiWidget (jordanUnitWidget[i]) ;
  466.       cols[i]  = ui_xIntFromAsciiWidget (jordanColWidget[i])  ;
  467.     } /*for*/
  468.  
  469.  
  470.     /* ------------------------  check values  ----------------------------- */
  471.  
  472.     for (i=0; i<3; i++)
  473.     {
  474.       if (units[i] < 1)
  475.       {
  476.         sprintf (buf,"Number of units has to be greater than 0 in line %d!",
  477.                  i + 1) ;
  478.         ui_confirmOk (buf);
  479.         return ;
  480.       } 
  481.  
  482.       if (cols[i] < 1)
  483.       {
  484.         sprintf (buf,"Number of cols has to be greater than 0 in line %d!",
  485.                  i + 1);
  486.         ui_confirmOk (buf) ;
  487.         return ;
  488.       } 
  489.     }
  490.  
  491.  
  492.     /* -------------------------  create net  ------------------------------ */
  493.  
  494.     if (bn_jordan_createNet (units[0], units[1], units[2], cols[0] , 
  495.                              cols[1] , cols[2] ) != KRERR_NO_ERROR) 
  496.       ui_confirmOk ("create net :  internal error") ;
  497.      
  498.     bn_basics_refresh () ;
  499.   } 
  500.  
  501.   return ;
  502. }
  503.  
  504.  
  505.  
  506.  
  507. /*****************************************************************************
  508.   FUNCTION : bn_create_jordan
  509.  
  510.   PURPOSE  : creates the 'BigNet (Jordan)' window
  511.   RETURNS  : void
  512.   NOTES    :
  513.  
  514.   UPDATE   : 
  515. ******************************************************************************/
  516.  
  517. void bn_create_jordan(void) 
  518.  
  519. {
  520.   Widget      jordan_panel, jordan_form, button, w_left, w_top ;
  521.   Arg          args[10];
  522.   Cardinal    n ;
  523.   char        buf[40];
  524.   int         i;
  525.   char        Name[4][20];
  526.  
  527.   
  528.   strcpy (Name[0], "Input  Layer: ") ;
  529.   strcpy (Name[1], "Hidden Layer: ") ;
  530.   strcpy (Name[2], "Output Layer: ") ;
  531.  
  532.  
  533.   if (bn_jordan_open == 0)
  534.   {
  535.     baseWidget   = 
  536.       XtCreatePopupShell ("BigNet (Jordan)", topLevelShellWidgetClass,
  537.                            ui_toplevel, NULL, 0) ;
  538.  
  539.     jordan_form  =
  540.       XtCreateManagedWidget ("box" , boxWidgetClass , baseWidget , NULL, 0) ;
  541.  
  542.     jordan_panel =
  543.       XtCreateManagedWidget ("form", formWidgetClass, jordan_form, NULL, 0) ;
  544.   
  545.  
  546.     /* ----------------------------  headline  -----------------------------*/
  547.      
  548.     w_left = w_top =
  549.       ui_xCreateLabelItem (" "           , jordan_panel, 12*8, NULL  , NULL);
  550.     w_left = 
  551.       ui_xCreateLabelItem ("No. of Units", jordan_panel, 12*8, w_left, NULL);
  552.     w_left = 
  553.       ui_xCreateLabelItem ("No. of Col." , jordan_panel, 12*8, w_left, NULL);
  554.  
  555.  
  556.     /* --------------------------  dialog rows  ----------------------------*/
  557.  
  558.     for (i = 0 ; i < 3 ; i++)
  559.     {
  560.       w_left =
  561.         ui_xCreateLabelItem  (Name[i], jordan_panel, 12*8, NULL  , w_top) ;
  562.   
  563.       w_left = 
  564.         ui_xCreateLabelItem  (" "    , jordan_panel,  2*8, w_left, w_top) ;
  565.  
  566.       w_left = jordanUnitWidget[i] = 
  567.         ui_xCreateDialogItem (" ", jordan_panel, "" , 6*8, w_left, w_top) ;
  568.  
  569.       w_left =
  570.         ui_xCreateLabelItem  (" "    , jordan_panel,  5*8, w_left, w_top) ;
  571.  
  572.       w_left = jordanColWidget[i]  = 
  573.         ui_xCreateDialogItem (" ", jordan_panel, "1", 6*8, w_left, w_top) ;
  574.   
  575.       w_top  = w_left ;
  576.     } 
  577.        
  578.  
  579.     /* -----------------------------  buttons  -----------------------------*/
  580.  
  581.     button =
  582.       bn_basics_xCreateButtonItem ("create_net", jordan_form, NULL  , w_top) ;
  583.       XtAddCallback (button, XtNcallback,
  584.                      (XtCallbackProc) bn_jordan_createPROC, NULL) ; 
  585.  
  586.     button =
  587.       bn_basics_xCreateButtonItem ("done"      , jordan_form, button, w_top) ;
  588.       XtAddCallback (button, XtNcallback,
  589.                      (XtCallbackProc) bn_jordan_donePROC  , NULL) ; 
  590.  
  591.     ui_checkWindowPosition(baseWidget) ;
  592.     XtPopup (baseWidget, XtGrabNone) ;
  593.  
  594.     fixWindowSize (baseWidget) ;
  595.  
  596.     bn_jordan_open = 1 ;
  597.   }
  598.   else
  599.   {
  600.     ui_confirmOk ("BigNet (Jordan) already loaded") ;
  601.   } 
  602.   
  603. }
  604.  
  605.  
  606.  
  607.  
  608. /*****************************************************************************
  609.   FUNCTION : bn_elman_createNet
  610.  
  611.   PURPOSE  : generates the ELMAN network 
  612.   RETURNS  : kernel error code
  613.   NOTES    :
  614.  
  615.   UPDATE   : 
  616. ******************************************************************************/
  617.  
  618. static krui_err bn_elman_createNet (void)
  619.  
  620. {
  621.   krui_err  error_code = KRERR_NO_ERROR;
  622.   int       i_unit, o_unit ;
  623.   int       h_unit[MAX_NO_OF_LAYERS], c_unit[MAX_NO_OF_LAYERS] ;
  624.   int       i, no_of_context_layers ;
  625.   int       no_of_hidden_layers, o_layer ;
  626.   int       dx, cx, cy ;
  627.   char      name[10] ;
  628.  
  629.  
  630.   no_of_hidden_layers = no_of_layers - 2 ;
  631.   o_layer             = no_of_layers - 1 ;   /* no. of the output layer */
  632.  
  633.  
  634.   /* ---------------------------   create units  ----------------------------*/
  635.  
  636.   i_unit = createUnitLayer (layer[0], INPUT, "Act_Identity", 
  637.                             "Out_Identity", "inp") ;
  638.   if (i_unit < 0) IF_ERROR_RETURN (i_unit) ;
  639.  
  640.   strcpy (name, "hid") ;
  641.   for (i = 1 ; i <= no_of_hidden_layers ; i++)
  642.   {
  643.     if (no_of_hidden_layers > 1) sprintf (name, "hid%d", i) ;  
  644.     h_unit[i] = createUnitLayer(layer[i], HIDDEN, "Act_Logistic", 
  645.                                 "Out_Identity", name) ;
  646.     if (h_unit[i] < 0) IF_ERROR_RETURN (h_unit[i]) ;
  647.   }
  648.  
  649.   o_unit = createUnitLayer (layer[o_layer], OUTPUT,  "Act_Logistic", 
  650.                             "Out_Identity", "out") ;
  651.   if (o_unit < 0) IF_ERROR_RETURN (o_unit) ;
  652.  
  653.   if (out_context) 
  654.   {
  655.     no_of_context_layers = no_of_hidden_layers + 1 ;
  656.     h_unit[no_of_context_layers] = o_unit ;
  657.   }
  658.   else 
  659.     no_of_context_layers = no_of_hidden_layers ;
  660.  
  661.   strcpy (name, "con") ;
  662.   for (i = 1 ; i <= no_of_context_layers ; i++)
  663.   {
  664.     if (no_of_context_layers > 1) sprintf (name, "con%d", i) ;  
  665.     c_unit[i] = createUnitLayer (layer[i], SPECIAL_H, "Act_Identity", 
  666.                                  "Out_Identity", name ) ;
  667.     if (c_unit[i] < 0) IF_ERROR_RETURN (c_unit[i]) ;
  668.   }
  669.  
  670.  
  671.   /* ---------------------------   create links  ----------------------------*/
  672.  
  673.   error_code = connectFull (i_unit, layer[0], h_unit[1], layer[1], 0.0) ;
  674.   IF_ERROR_RETURN (error_code) ;
  675.  
  676.   for (i = 1 ; i < no_of_hidden_layers ; i++)
  677.   {
  678.     error_code = 
  679.       connectFull (h_unit[i], layer[i], h_unit[i+1], layer[i+1], 0) ;
  680.     IF_ERROR_RETURN (error_code) ;
  681.   }
  682.  
  683.   error_code = connectFull (h_unit[i], layer[i], o_unit, layer[o_layer], 0) ;
  684.   IF_ERROR_RETURN (error_code) ;
  685.   
  686.   for (i = 1 ; i <= no_of_context_layers ; i++)
  687.   {
  688.     error_code = connectFull (c_unit[i], layer[i],  h_unit[i], layer[i], 0) ;
  689.     IF_ERROR_RETURN (error_code) ;
  690.  
  691.     error_code = connectOneToOne (h_unit[i], c_unit[i], layer[i], 0) ;
  692.     IF_ERROR_RETURN (error_code) ;
  693.  
  694.     error_code = connectSelfRec (c_unit[i], layer[i], 0) ;
  695.     IF_ERROR_RETURN (error_code) ;
  696.   }
  697.   
  698.  
  699.   /* --------------------------   set positions  ----------------------------*/
  700.  
  701.   cx = 1 ; /* position of the upper left */
  702.   cy = 1 ; /* corner of a context layer  */
  703.  
  704.   for (i = 0 ; i < no_of_layers ; i++)
  705.   {
  706.     if ((int) (layer[i] / columns[i]) > cy - 1)
  707.       cy = 1 + (int) (layer[i] / columns[i]) ;
  708.   }
  709.  
  710.   cy += 1 ;
  711.  
  712.   dx  = 1 ; /* x position of the upper left corner of an input, */
  713.             /* hidden or output layer                           */
  714.  
  715.   setPosRow (i_unit, layer[0], 1, 1, columns[0]) ;
  716.  
  717.   cx = dx + columns[0] + 2 ;
  718.   dx = cx + columns[1] + 2 ;
  719.  
  720.   for (i = 1 ; i <= no_of_context_layers ; i++)
  721.   {
  722.     setPosRow (h_unit[i], layer[i],  dx, 1 , columns[i]) ;
  723.     setPosRow (c_unit[i], layer[i],  cx, cy, columns[i]) ;
  724.  
  725.     cx = dx + columns[i]   + 2 ;
  726.     dx = cx + columns[i+1] + 2 ;
  727.   }
  728.  
  729.   if (! out_context)
  730.     setPosRow (o_unit, layer[o_layer], dx, 1, columns[o_layer]) ;
  731.  
  732.  
  733.   /* ----------------------   set default functions  ------------------------*/
  734.  
  735.   error_code = krui_setLearnFunc ("JE_BP") ;
  736.   IF_ERROR_RETURN (error_code) ;
  737.  
  738.   error_code = krui_setInitialisationFunc ("JE_Weights") ;
  739.   IF_ERROR_RETURN (error_code) ;
  740.  
  741.   error_code = krui_setUpdateFunc ("JE_Order") ;
  742.  
  743.   return (error_code) ;
  744. }
  745.  
  746.  
  747.  
  748. /*****************************************************************************
  749.   FUNCTION : bn_elman_getFromWidget 
  750.  
  751.   PURPOSE  : get values from 'BigNet (Elman)' window
  752.   RETURNS  : void
  753.   NOTES    :
  754.  
  755.   UPDATE   : 
  756. ******************************************************************************/
  757.  
  758. static void bn_elman_getFromWidget (void)
  759.  
  760. {
  761.   int i ;
  762.  
  763.   for (i = 0 ; i < no_of_layers ; i++)
  764.   {
  765.     layer[i]   = ui_xIntFromAsciiWidget (elmanUnitWidget[i]) ;
  766.     columns[i] = ui_xIntFromAsciiWidget (elmanColWidget[i] ) ;
  767.   }
  768.  
  769.   out_context = ui_xGetToggleState (t_yes) ;
  770. }
  771.  
  772.  
  773.  
  774. /*****************************************************************************
  775.   FUNCTION : bn_elman_donePROC
  776.  
  777.   PURPOSE  : callback function for DONE button in 'BigNet (Elman)'
  778.   RETURNS  : void
  779.   NOTES    :
  780.  
  781.   UPDATE   : 
  782. ******************************************************************************/
  783.  
  784. static void bn_elman_donePROC (void)
  785.  
  786. {
  787.    XtDestroyWidget (baseWidget) ;
  788.    bn_elman_open = 0 ;
  789. }
  790.  
  791.  
  792.  
  793. /*****************************************************************************
  794.   FUNCTION : bn_elman_createPROC
  795.  
  796.   PURPOSE  : callback function for CREATE NET button in 'BigNet (Elman)'
  797.   RETURNS  : void
  798.   NOTES    :
  799.  
  800.   UPDATE   : 
  801. ******************************************************************************/
  802.  
  803. static void bn_elman_createPROC (void)
  804.  
  805. {
  806.   int  i       ;
  807.   char buf[80] ;
  808.  
  809.  
  810.   if (bn_basics_check_existingNetwork())
  811.   {
  812.     bn_elman_getFromWidget () ;
  813.  
  814.     for (i = 0 ; i < no_of_layers ; i++)
  815.     {
  816.  
  817.       /* -------------------------  check values  -------------------------- */
  818.  
  819.       if (layer[i] < 1)
  820.       {
  821.         sprintf (buf, "Number of units has to be greater than 0 in line %d!",
  822.                  i + 1) ;
  823.         ui_confirmOk (buf) ;
  824.         return ;
  825.       } 
  826.  
  827.       if (columns[i] < 1)
  828.       {
  829.         sprintf (buf, "Number of columns has to be greater than 0 in line %d!",
  830.                  i + 1) ;
  831.         ui_confirmOk (buf) ;
  832.         return ;
  833.       } 
  834.     }
  835.    
  836.    if (bn_elman_createNet () != KRERR_NO_ERROR)
  837.      ui_confirmOk ("create net :  internal error") ;
  838.  
  839.    bn_basics_refresh  () ;
  840.   } 
  841.  
  842.   return ;
  843. }
  844.  
  845.  
  846.  
  847. /*****************************************************************************
  848.   FUNCTION : bn_elman_create_dialogrow
  849.  
  850.   PURPOSE  : creates a dialog row in the 'BigNet (Elman)' window
  851.   RETURNS  : void
  852.   NOTES    :
  853.  
  854.   UPDATE   : 
  855. ******************************************************************************/
  856.  
  857. static void bn_elman_create_dialogrow (int i, char *str) 
  858.  
  859. {
  860.   Widget w_top, w_left ;
  861.   char   buf[256] ;
  862.  
  863.  
  864.   if (i == 0) w_top = w_headline ;
  865.   else        w_top = elmanLabelWidget [i - 1] ;
  866.  
  867.   sprintf (buf, "  %3d      %s", i+1, str) ;
  868.  
  869.   w_left = elmanLabelWidget[i] =
  870.     ui_xCreateLabelItem (buf, elman_form[0], 19 * 8, NULL, w_top) ;
  871.  
  872.   if (layer[i] == 0) strcpy  (buf, "") ;
  873.   else               sprintf (buf, "%d", layer[i]) ;
  874.  
  875.   w_left = elmanUnitWidget[i] = 
  876.     ui_xCreateDialogItem (" ", elman_form[0], buf, 6 * 8, w_left, w_top) ;
  877.  
  878.   w_left = elmanSpaceWidget[i] =
  879.     ui_xCreateLabelItem  (" ", elman_form[0], 5 * 8, w_left, w_top) ;
  880.  
  881.   if (columns[i] == 0) strcpy  (buf, "") ;
  882.   else                 sprintf (buf, "%d", columns[i]) ;
  883.  
  884.   w_left = w_top = elmanColWidget[i]  = 
  885.     ui_xCreateDialogItem (" ", elman_form[0], buf, 6 * 8, w_left, w_top) ;
  886.  
  887. }
  888.  
  889.  
  890.  
  891. /*****************************************************************************
  892.   FUNCTION : bn_elman_yesPROC
  893.  
  894.   PURPOSE  : callback function for YES button in 'BigNet (Elman)'
  895.   RETURNS  : void
  896.   NOTES    :
  897.  
  898.   UPDATE   :
  899. ******************************************************************************/
  900.  
  901. static void bn_elman_yesPROC (void)
  902.  
  903. {
  904.   if (! ui_xGetToggleState (t_yes)) ui_xSetToggleState (t_yes, TRUE ) ;
  905.   else                              ui_xSetToggleState (t_no , FALSE) ;
  906. }
  907.  
  908.  
  909.  
  910. /*****************************************************************************
  911.   FUNCTION : bn_elman_noPROC
  912.  
  913.   PURPOSE  : callback function for NO button in 'BigNet (Elman)'
  914.   RETURNS  : void
  915.   NOTES    :
  916.  
  917.   UPDATE   :
  918. ******************************************************************************/
  919.  
  920. static void bn_elman_noPROC (void)
  921.  
  922. {
  923.   if (! ui_xGetToggleState (t_no)) ui_xSetToggleState (t_no , TRUE ) ;
  924.   else                             ui_xSetToggleState (t_yes, FALSE) ;
  925. }
  926.  
  927.  
  928.  
  929. /*****************************************************************************
  930.   FUNCTION : bn_elman_insertPROC
  931.  
  932.   PURPOSE  : callback function for INSERT button in 'BigNet (Elman)'
  933.              inserts a new hidden layer
  934.   RETURNS  : void
  935.   NOTES    :
  936.  
  937.   UPDATE   :
  938. ******************************************************************************/
  939.  
  940. static void bn_elman_insertPROC (void)
  941.  
  942. {
  943.   if (no_of_layers < MAX_NO_OF_LAYERS)
  944.   {
  945.     bn_elman_getFromWidget () ;
  946.  
  947.     layer[no_of_layers]     = layer[no_of_layers - 1] ;
  948.     layer[no_of_layers - 1] = 0 ;
  949.  
  950.     columns[no_of_layers]     = columns[no_of_layers - 1] ;
  951.     columns[no_of_layers - 1] = 1 ;
  952.  
  953.  
  954.     /* ----------    delete output layer     ---------- */
  955.  
  956.     XtDestroyWidget (elmanLabelWidget[no_of_layers - 1]) ;
  957.     XtDestroyWidget (elmanUnitWidget [no_of_layers - 1]) ;
  958.     XtDestroyWidget (elmanSpaceWidget[no_of_layers - 1]) ;
  959.     XtDestroyWidget (elmanColWidget  [no_of_layers - 1]) ;
  960.  
  961.  
  962.     /* -------  insert hidden and output layer  ------- */
  963.  
  964.     no_of_layers++ ;
  965.     bn_elman_create_dialogrow (no_of_layers - 2, "hidden") ;
  966.     bn_elman_create_dialogrow (no_of_layers - 1, "output") ;
  967.  
  968.     fixWindowSize (baseWidget) ;
  969.   }
  970.   else
  971.     ui_confirmOk ("No more hidden layers are supported !") ;
  972. }
  973.  
  974.  
  975.  
  976. /*****************************************************************************
  977.   FUNCTION : bn_elman_deletePROC
  978.  
  979.   PURPOSE  : callback function for DELETE button in 'BigNet (Elman)'
  980.              deletes the last hidden layer
  981.   NOTES    :
  982.  
  983.   UPDATE   :
  984. ******************************************************************************/
  985.  
  986. static void bn_elman_deletePROC (void)
  987.  
  988. {
  989.   if (no_of_layers > 3)
  990.   {
  991.     bn_elman_getFromWidget () ;
  992.  
  993.     no_of_layers-- ;
  994.  
  995.     layer[no_of_layers - 1]   = layer[no_of_layers] ;
  996.     layer[no_of_layers]       = 0 ;
  997.  
  998.     columns[no_of_layers - 1] = columns[no_of_layers] ;
  999.     columns[no_of_layers]     = 1 ;
  1000.  
  1001.  
  1002.     /* ----------  delete last hidden layer  ---------- */
  1003.  
  1004.     XtDestroyWidget (elmanLabelWidget[no_of_layers - 1]) ;
  1005.     XtDestroyWidget (elmanUnitWidget [no_of_layers - 1]) ;
  1006.     XtDestroyWidget (elmanSpaceWidget[no_of_layers - 1]) ;
  1007.     XtDestroyWidget (elmanColWidget  [no_of_layers - 1]) ;
  1008.  
  1009.  
  1010.     /* ----------    delete output layer     ---------- */
  1011.  
  1012.     XtDestroyWidget (elmanLabelWidget[no_of_layers]) ;
  1013.     XtDestroyWidget (elmanUnitWidget [no_of_layers]) ;
  1014.     XtDestroyWidget (elmanSpaceWidget[no_of_layers]) ;
  1015.     XtDestroyWidget (elmanColWidget  [no_of_layers]) ;
  1016.  
  1017.  
  1018.     /* ----------    insert output layer     ---------- */
  1019.  
  1020.     bn_elman_create_dialogrow (no_of_layers - 1, "output") ;
  1021.  
  1022.     fixWindowSize (baseWidget) ;
  1023.   }
  1024.   else
  1025.     ui_confirmOk ("You can not delete the last hidden layer !") ; 
  1026. }
  1027.  
  1028.  
  1029.  
  1030. /*****************************************************************************
  1031.   FUNCTION : bn_create_elman
  1032.  
  1033.   PURPOSE  : creates the 'BigNet (Elman)' window
  1034.   RETURNS  : void
  1035.   NOTES    :
  1036.  
  1037.   UPDATE   : 
  1038. ******************************************************************************/
  1039.  
  1040. void bn_create_elman (void)
  1041.  
  1042. {
  1043.   Widget      button, w_left, w_top ;
  1044.   Cardinal    n ;
  1045.   Arg          args[10];
  1046.   char        buf[20];
  1047.   int         i;
  1048.  
  1049.  
  1050.   if (bn_elman_open == 0)
  1051.   {
  1052.     no_of_layers = 3 ;                  /* 1 input, 1 hidden, 1 output layer */
  1053.  
  1054.     for (i = 0 ; i < MAX_NO_OF_LAYERS ; i++)
  1055.     {
  1056.       layer[i]   = 0 ;
  1057.       columns[i] = 1 ;
  1058.     }
  1059.  
  1060.     baseWidget    = XtCreatePopupShell ("BigNet (Elman)",
  1061.                                         topLevelShellWidgetClass,
  1062.                                         ui_toplevel, NULL, 0);
  1063.  
  1064.     elman_box     = XtCreateManagedWidget ("box" , boxWidgetClass , 
  1065.                                            baseWidget, NULL , 0) ;
  1066.  
  1067.     elman_form[0] = XtCreateManagedWidget ("form", formWidgetClass, 
  1068.                                            elman_box, NULL , 0) ;
  1069.  
  1070.  
  1071.     /* -----------------------------  headline  ----------------------------*/
  1072.  
  1073.     w_left = w_top = w_headline =
  1074.       ui_xCreateLabelItem ("Layer No.  Type    No. of Units  No. of Col.  ",
  1075.                            elman_form[0], (9 + 8 + 24) * 8, NULL  , NULL) ;
  1076.  
  1077.  
  1078.     /* ---------------------------  dialog rows  ---------------------------*/
  1079.  
  1080.     for (i = 0 ; i < no_of_layers ; i++)
  1081.     {
  1082.       if      (i == 0)              bn_elman_create_dialogrow (i, "input ") ;
  1083.       else if (i == no_of_layers-1) bn_elman_create_dialogrow (i, "output") ;
  1084.       else                          bn_elman_create_dialogrow (i, "hidden") ;
  1085.     } 
  1086.  
  1087.  
  1088.     /* -----------------  output context :  yes / no  ----------------------*/
  1089.  
  1090.     elman_form[1] = XtCreateManagedWidget ("form", formWidgetClass, elman_box,
  1091.                        NULL , 0) ;
  1092.  
  1093.     w_left = ui_xCreateLabelItem ("Output Context :", elman_form[1], 
  1094.                                   20 * 8, NULL, NULL) ;
  1095.  
  1096.     t_yes =
  1097.       ui_xCreateToggleItem ("yes", elman_form[1], NULL, w_left, NULL) ;
  1098.       XtAddCallback (t_yes, XtNcallback,
  1099.                      (XtCallbackProc) bn_elman_yesPROC, NULL) ; 
  1100.  
  1101.     w_left = ui_xCreateLabelItem (" ", elman_form[1], 7 * 8, t_yes, 
  1102.                                   NULL) ;
  1103.  
  1104.     t_no =
  1105.       ui_xCreateToggleItem ("no", elman_form[1], NULL, w_left, NULL) ;
  1106.       XtAddCallback (t_no, XtNcallback,
  1107.                      (XtCallbackProc) bn_elman_noPROC, NULL) ; 
  1108.  
  1109.     w_left = ui_xCreateLabelItem (" ", elman_form[1], 3 * 8 + 4, t_no, 
  1110.                                   NULL) ;
  1111.  
  1112.     ui_xSetToggleState (t_yes, FALSE) ;
  1113.     ui_xSetToggleState (t_no , TRUE ) ;
  1114.  
  1115.  
  1116.     /* --------------  hidden layers :  insert / delete  -------------------*/
  1117.  
  1118.     elman_form[2] = XtCreateManagedWidget ("form", formWidgetClass, elman_box,
  1119.                        NULL , 0) ;
  1120.  
  1121.     w_left = ui_xCreateLabelItem ("Hidden Layers  :", elman_form[2], 
  1122.                                   18 * 8 + 5, NULL, NULL) ;
  1123.  
  1124.     button =
  1125.       bn_basics_xCreateButtonItem ("insert", elman_form[2], w_left, NULL) ;
  1126.       XtAddCallback (button, XtNcallback,
  1127.                      (XtCallbackProc) bn_elman_insertPROC, NULL) ; 
  1128.  
  1129.     w_left = ui_xCreateLabelItem (" ", elman_form[2], 4 * 8 + 5, button, 
  1130.                                   NULL) ;
  1131.  
  1132.     button =
  1133.       bn_basics_xCreateButtonItem ("delete", elman_form[2], w_left, NULL) ;
  1134.       XtAddCallback (button, XtNcallback,
  1135.                      (XtCallbackProc) bn_elman_deletePROC, NULL) ; 
  1136.  
  1137.     w_left = ui_xCreateLabelItem (" ", elman_form[2], 2 * 8 + 4, button, 
  1138.                                   NULL) ;
  1139.  
  1140.  
  1141.     /* -----------------------  done / create  -----------------------------*/
  1142.  
  1143.     button = 
  1144.       bn_basics_xCreateButtonItem ("create_net", elman_box, NULL  , 
  1145.                                    elman_form[2]) ;
  1146.       XtAddCallback (button, XtNcallback,
  1147.                      (XtCallbackProc) bn_elman_createPROC, NULL) ; 
  1148.  
  1149.     button =
  1150.       bn_basics_xCreateButtonItem ("done"      , elman_box, button,
  1151.                                    elman_form[2]) ;
  1152.       XtAddCallback (button, XtNcallback,
  1153.                      (XtCallbackProc) bn_elman_donePROC  , NULL) ; 
  1154.  
  1155.     ui_checkWindowPosition (baseWidget) ;
  1156.     XtPopup (baseWidget, XtGrabNone) ; 
  1157.  
  1158.     fixWindowSize (baseWidget) ;
  1159.  
  1160.     bn_elman_open = 1 ;
  1161.   }
  1162.   else
  1163.     ui_confirmOk ("BigNet (Elman) already loaded!") ;
  1164. }
  1165.  
  1166.  
  1167.  
  1168. /*****************************************************************************
  1169.                         E N D     O F     F I L E
  1170. ******************************************************************************/
  1171.  
  1172.  
  1173.  
  1174.  
  1175.